package com.marvels.approve.process.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.marvels.approve.common.utils.ApplicationContextAchieve;
import com.marvels.approve.loan.dto.ZxDataRespDTO;
import com.marvels.approve.process.dao.CreditReportingDao;
import com.marvels.approve.process.dto.CreditReportingDTO;
import com.marvels.approve.process.dto.ProductCreditDataDTO;
import com.marvels.approve.process.service.CreditDataService;
import com.marvels.approve.process.service.CreditReportingService;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;

@Service("creditReportingService")
public class CreditReportingServiceImpl implements CreditReportingService {
	@Autowired
	private CreditReportingDao creditReportingDao;
	@Override
	public List<ProductCreditDataDTO> getProductCreditData(String loanId) {
		List<ProductCreditDataDTO> list = creditReportingDao.getProductCreditData(loanId);
		if (CollectionUtils.isNotEmpty(list)) {
			for (int i=0; i<list.size(); i++) {
				String serviceType = list.get(i).getServiceType();
				CreditDataService creditData = (CreditDataService) ApplicationContextAchieve.getApplicationContext().getBean(serviceType);
				try {
					ZxDataRespDTO datas= creditData.execute(loanId);
					list.get(i).setDatas(datas);
				} catch (MarvelsBusinessException e) {
					MarvelsLogUtil.getInstance().error("",e);
				}
			}
		}
		return list;
	}
	/**
	 * 查看某个机构的征信数据明细
	 * reqNo：查询明细请求号
	 * serviceType:决定调用哪个service类
	 * loanId
	 */
	@Override
	public ZxDataRespDTO getCreditDetail(ProductCreditDataDTO productCreditDataDTO) {
		String serviceType = productCreditDataDTO.getServiceType();
		String loanId = productCreditDataDTO.getLoanId();
		String reqNo = productCreditDataDTO.getReqNo();
		ZxDataRespDTO datas= new ZxDataRespDTO();
		if (!StringUtils.isEmpty(serviceType)) {
			CreditDataService creditData = (CreditDataService) ApplicationContextAchieve.getApplicationContext().getBean(serviceType);
			try {
				datas= creditData.getCreditDetail(reqNo, loanId);
			} catch (MarvelsBusinessException e) {
				MarvelsLogUtil.getInstance().error("",e);
			}
		}
		return datas;
	}

}
